<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>例子21-ES6/7常用语法介绍</title>
    </head>
    <body>
        <pre>
            <code>
                // 例子21

                console.log(1, ...[2, 3, 4], 5); // 1 2 3 4 5

                //--------------------------------------------------------------------------

                [...document.querySelectorAll('div')]; // [&lt;div&gt;, &lt;div&gt;, &lt;div&gt;, ..., &lt;div&gt;]

                //--------------------------------------------------------------------------

                function add(x, y) {
                    return x + y;
                }

                const numbers = [1, 2];
                add(...numbers); // 3

                //--------------------------------------------------------------------------

                // 替换apply方法
                const numbers = [1, 10, 5, 0];
                // ES5写法
                Math.max.apply(null, numbers); // 10

                // ES6语法
                Math.max(...numbers); // 10

                //--------------------------------------------------------------------------

                // 替换apply方法
                // ES5的写法
                var arr1 = [0, 1, 2];
                var arr2 = [3, 4, 5];
                Array.prototype.push.apply(arr1, arr2);
                console.log(arr1); // [0, 1, 2, 3, 4, 5]

                // ES6的写法
                let arr1 = [0, 1, 2];
                let arr2 = [3, 4, 5];
                arr1.push(...arr2);
                console.log(arr1); // [0, 1, 2, 3, 4, 5]
            </code>
        </pre>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/21.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>
</html>